package viz.view.mediators
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import mx.core.Application;
	
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class KeyStrokeMediator extends Mediator
	{
		public static const NAME:String = "KeyStrokeMediator";
		public static const KEYSTROKE_EDIT:String = "keystroke/edit";
		public static const KEYSTROKE_OPEN:String = "keystroke/open";
		public static const KEYSTROKE_SAVE:String = "keystroke/save";
		public static const KEYSTROKE_EXPORT:String = "keystroke/export";
		public static const KEYSTROKE_ESCAPE:String = "keystroke/escape";
		public static const KEYSTROKE_ENTER:String = "keystroke/enter";
		public static const KEYSTROKE_CONTROL_DELETE:String = "keystroke/delete/control";
		public static const KEYSTROKE_A_CONTROL:String = "keystroke/a/control";
		
		
		public static const KEYSTROKE_CONTROL_DOWN:String = "keystroke/control/down";
		public static const KEYSTROKE_CONTROL_UP:String = "keystroke/control/up";
		
		private var data:XML
		

		public function KeyStrokeMediator( viewComponent:Object )
		{
			super(NAME, viewComponent );
			initMediator()
			
		}
		
		public function setData( data:Object ):void{
			this.data = XML( data );
		}
		
		private function initMediator():void{
			view.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			view.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown)
		}
		
		private function onKeyUp(event:KeyboardEvent):void{
			switch(true){
				case(event.ctrlKey && event.keyCode == Keyboard.E):
					sendKeyStrokeNote( KEYSTROKE_EDIT );
					break;
					
				case(event.ctrlKey && event.keyCode == Keyboard.O):
					sendKeyStrokeNote( KEYSTROKE_OPEN );
					break;
				
				case(event.ctrlKey && event.keyCode == Keyboard.S):
					sendKeyStrokeNote( KEYSTROKE_SAVE );
					break;
				
				case(event.ctrlKey && event.keyCode == Keyboard.A):
					sendKeyStrokeNote( KEYSTROKE_A_CONTROL );
					break;
					
				case(event.keyCode == Keyboard.CONTROL):
					sendKeyStrokeNote( KEYSTROKE_CONTROL_UP );
					break;
					
					
				case(event.keyCode == Keyboard.ESCAPE):
					sendKeyStrokeNote( KEYSTROKE_ESCAPE );
					break;
					
				case(event.ctrlKey && event.keyCode == Keyboard.DELETE):
					sendKeyStrokeNote( KEYSTROKE_CONTROL_DELETE );
					break;
					
				case(event.keyCode == Keyboard.ENTER):
					sendKeyStrokeNote( KEYSTROKE_ENTER);
					break;
			}
		}
		
		private function onKeyDown(event:KeyboardEvent):void{
			switch(true){
				case(event.keyCode == Keyboard.CONTROL):
					sendKeyStrokeNote( KEYSTROKE_CONTROL_DOWN );
					break;
			}
		}
		
		private function sendKeyStrokeNote( name:String ):void{
			sendNotification( name );
		}
		
		private function get view():Stage{
			return Stage( viewComponent );
		}
		
		
		
	}
}